<template>
  <el-container>
    <el-header class="menu-header">
      <el-menu mode="horizontal" router :ellipsis="false" :default-active="activeMenu">
        <el-menu-item index="/home">
          <template #title>
            <HomeFilled style="width: 1em; height: 1em; margin-right: 8px" />
            主页
          </template>
        </el-menu-item>
        <el-menu-item index="/todo">
          <template #title>
            <List style="width: 1em; height: 1em; margin-right: 8px" />
            待办事项
          </template>
        </el-menu-item>
        <el-sub-menu index="/user">
          <template #title>
            <UserFilled style="width: 1em; height: 1em; margin-right: 8px" />
            {{ usernameStore.username }}
          </template>
          <el-menu-item index="/login" @click="handleLogout">
            <template #title>
              <SwitchButton style="width: 1em; height: 1em; margin-right: 8px" />
              退出登录
            </template>
          </el-menu-item>
          <el-menu-item index="/settings">
            <template #title>
              <Tools style="width: 1em; height: 1em; margin-right: 8px" />
              用户设置
            </template>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-header>
    <el-main class="router-view-container">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </el-main>
    <el-footer>
      <CopyRight />
    </el-footer>
  </el-container>
</template>

<script setup>
import { HomeFilled, List, UserFilled, SwitchButton, Tools } from '@element-plus/icons-vue'
import CopyRight from '@/views/copyright/index.vue'
import { useUsernameStore } from './stores/usernameStore'
import { computed } from 'vue'
import { useRoute } from 'vue-router'

const usernameStore = useUsernameStore()
const route = useRoute()

// 计算当前激活的菜单项
const activeMenu = computed(() => {
  return route.path
})

const handleLogout = () => {
  usernameStore.clearUsername()
}
</script>

<style scoped>
.el-menu--horizontal > .el-menu-item:nth-last-child(2) {
  margin-right: auto;
}

.router-view-container {
  min-height: 88vh;
  padding: 20px;
}

.menu-header {
  position: sticky;
  top: 0;
  z-index: 1000;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
